package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.*;
import com.sky.result.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.lang.String;

@Service
@Slf4j
public class CategoryServiceImpl {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private dish_flavorMapper dfMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private Setmeal_dishMapper setmeal_dishMapper;
    public void add(CategoryDTO dto){
        Category category = new Category();
        BeanUtils.copyProperties(dto,category);
        category.setStatus(StatusConstant.DISABLE);
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateUser(BaseContext.getCurrentId());
        category.setCreateUser(BaseContext.getCurrentId());
        category.setUpdateTime(LocalDateTime.now());
        //以上为属性注入
        categoryMapper.addCategory(category);
    }

    public PageResult getbypage(CategoryPageQueryDTO dto){
        PageHelper.startPage(dto.getPage(),dto.getPageSize());
        Page<Category> page = categoryMapper.getBypage(dto);

        Long totals = page.getTotal();
        List<Category> records = page.getResult();
        return new PageResult(totals,records);
    }

    public void startorstop(Integer status,Long id){
        Category category = new Category();
        category.setId(id);
        category.setStatus(status);
        categoryMapper.start_or_stop(category);
    }

    public void update(CategoryDTO dto){
        Category category = new Category();
        BeanUtils.copyProperties(dto,category);
        category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(BaseContext.getCurrentId());
        categoryMapper.update_(category);
    }

    public List<Category> getBytype(Integer type){
        return categoryMapper.getBytype_(type);
    }
    public List<Category> getBytype(){
        return categoryMapper.getByalltypes();
    }
    @Transactional
    //业务逻辑是：若分类关联了套餐或菜品，不能删；否则可以删
    public void deleteByid(String id) throws DeletionNotAllowedException{
        Long ID = Long.parseLong(id);//分类ID
        List<Long>ids = dishMapper.getdishesBycategoryId(ID);//先拿到删掉的分类下的所有菜品id
        String name = setmealMapper.getnamebycategoryid_(ID);//判断该分类下有没有至少一个套餐
        if(name == null && ids!=null){
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);//如果该分类关联了菜品，不能删除
        }
        else if( name != null ){
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
//            categoryMapper.delete_category(ID);//删掉分类
//            dishMapper.delete_dishesBycategoryId(ID);//删掉该分类下的所有菜品
//            dfMapper.delete_flavors(ids);//删掉所有菜品相关的口味
        }
        else {
            categoryMapper.delete_category(ID);
        }

//        else if(){
////            String [] IDs = setmealMapper.getidBycategoryid(ID);//拿到该分类下的所有套餐id;
////            setmealMapper.deleteByid(IDs);
////            setmeal_dishMapper.deletes(IDs);
        }



    }


